package CS4310.csueb.story;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import CS4310.csueb.characters.Hero;
import CS4310.csueb.visual.SpriteManager;

/**
 * Class to manage the progression of the story line. Interacts with
 * the game manager to determine if certain objectives are complete and
 * how much of the story has been completed.
 *
 * @author Levester Chaney
 *
 */
// Also debating whether I should instantiate a 
// mission class that contains multiple objectives.
public class QuestManager 
{
	private int current;
	private Mission missions[];
	private Scanner fileIn;
	
	/**
	 * Default constructor for testing purposes. Only initializes
	 * one mission.
	 */
	public QuestManager()
	{
		try{
			current = 0;
			fileIn = new Scanner(new File("CS4310/csueb/resource/quests.info"));
			initMissions();
		}catch(FileNotFoundException e){
		}
	}
	
	/**
	 * Takes in the Hero and SpriteManager to determine the completion
	 * of current objective and whether or not to progress the story
	 * 
	 * @param h the Hero of the game
	 * @param s the collection of characters in the game
	 */
	public void update(Hero h, SpriteManager s)
	{
		// if current objective satisfied, increment to next
		if(missions[current].isComplete(h, s))
		{
			current++;
		}
	}
	
	/**
	 * Retrieves the information pertaining to the currently
	 * unlocked game missions
	 * @return the current mission objective
	 */
	public Mission[] getCurrentMissions()
	{
		current = missions.length;
		Mission curr[];
		
		if(current == 0)
			curr = new Mission[1];
		else
			curr = new Mission[current];
		
		// loop through missions up to current
		for(int i = 0; i < curr.length; i++)
		{
			curr[i] = missions[i];
		}
		
		return curr;
	}
	
	// inititialzes all the game missions
	private void initMissions()
	{
		// initialize the mission list based on 1st line in file
		missions = new Mission[fileIn.nextInt()];
		
		// loop through the lines of the file to initialize quests
		while(fileIn.hasNext())
		{
			int lines = fileIn.nextInt(); //number of objectives in mission
			fileIn.nextLine();	//eats up whitespace after integer read
			Mission m = new Mission(lines-1);
			String s;
			for(int i = 0; i < lines;i++)
			{
				s = fileIn.nextLine();
				if(i == 0)
				{
					m.addTitle(s);
				}
				else
				{
					if(s.contains("Talk"))
						m.addObjective(new TalkObjective(s));
					else if(s.contains("Defeat all"))
						m.addObjective(new DefeatAllObjective(s));
					else if(s.contains("Defeat"))
						m.addObjective(new DefeatObjective(s));
					else
						m.addObjective(new CollectObjective(s));
				}
			}
			addMission(m);
		}
	}
	
	private void addMission(Mission m)
	{
		for(int i = 0; i < missions.length; i++)
		{
			if(missions[i] == null)
			{
				missions[i] = m;
				break;
			}
		}
	}
}
